package testdemo;

import dao.UserDao;
import dao.UserMapperDao;
import javabean.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import tools.ThingManage;

import java.util.List;

/**
 * @author 路老师
 * @date 2025/3/26
 * 无精神，不编程！无乐趣，不编程！
 * “调试：与bug共舞的艺术。”
 */
public class TestAnnotation {
    @Test
    public void findAllData()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<User> allUser = mapper.findAllUser();
        System.out.println("allUser = " + allUser);

    }


    @Test
    public void insertData()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        User user = new User();
        user.setUsername("小明同学");
        user.setPassword("1234567");
        user.setBirthday("1999年3月25日");
        mapper.saveUser(user);
        sqlSession.commit();
        sqlSession.close();
    }


    @Test
    public void UpdateData()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);

        User user = new User();
        user.setId(12);
        user.setUsername("小明同学666");
        user.setPassword("666");
        user.setBirthday("1999年3月25日");
        mapper.updateUser(user);
        sqlSession.commit();
        sqlSession.close();
    }


    @Test
    public void findUserByIdTest()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User userById = mapper.findUserById(12);
        System.out.println("userById = " + userById);
        sqlSession.close();
    }
}
